﻿Partial Public Class ActivityExplorer

    Public Overridable Sub TreeActionOptionListLoad()
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("Activity", "ACT_00066", "Last 90 Days", AddressOf MenuActivityLessThan90, True, True))
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("Activity", "ACT_00067", "Last 120 Days", AddressOf MenuActivityLessThan90, True, True))
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("Activity", "ACT_00068", "All", AddressOf MenuActivityLessThan90, True, True))
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("AddRoot", "ACT_00069", "New Activity", AddressOf MenuActivityLessThan90, True, True))
        'ACT_00208 for grid update
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("AddRoot", "ACT_00209", "Card Update", AddressOf Activity_GridLayout, True, True, 0))
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("??????", "???????", "Print Activity", AddressOf MenuPrintActivity, True, True))
    End Sub

    Public Overridable Sub InitNewActivity(sender As Object, e As InitNewRowEventArgs)
        Dim view As GridView = DirectCast(sender, GridView)
        Dim row As DataRow = view.GetFocusedDataRow()
        Using Activity As SystemBoxSDK.PL.CRMSDK.Project_PL = New SystemBoxSDK.PL.CRMSDK.Project_PL
            Activity.NewRecord(row)
        End Using
    End Sub

    Public Overridable Sub InitNewLayoutRow(sender As Object, e As InitNewRowEventArgs)
        Dim LayoutView As LayoutView = CType(sender, LayoutView)
        Dim row As DataRow = LayoutView.GetFocusedDataRow()
        Using Project As SystemBoxSDK.PL.CRMSDK.Project_PL = New SystemBoxSDK.PL.CRMSDK.Project_PL()
            Project.NewRecord(row)
        End Using
        row("Inactive") = 0
    End Sub

    Public Overridable Sub AfterInitView(ByVal sender As Object, ByVal gridView As GridView)
        Select Case gridView.Tag.ToString
            Case "ActivityDocument"
                'To make it work properly, The Security of the MetaControl MQ must be Read and Update only
                'So we dont have the Add Row.

                'No filter Row
                gridView.OptionsView.ShowAutoFilterRow = False
                'The height of the row
                gridView.RowHeight = 800
                'Will fill all the colums with automaticly
                gridView.OptionsView.ColumnAutoWidth = True
                'No Grouping
                gridView.OptionsView.ShowGroupPanel = False
                'No Filter 
                gridView.OptionsView.ShowFilterPanelMode = Views.Base.ShowFilterPanelMode.Never
            Case Else
        End Select

    End Sub



    Public Overridable Sub CustomRowCellEditForEditing(ByVal sender As Object, ByVal e As CustomRowCellEditEventArgs)
        Dim aView As GridView = DirectCast(sender, GridView)
        Dim aRTEdit As RepositoryItemButtonRichTextEditExt
        Dim aLov As New RepositoryItemGridLookUpEdit()

        Dim aRow As DataRow = aView.GetFocusedDataRow()

        Dim aDt As DataTable

        If Not IsNothing(aView.Columns("ProjectDocument")) Then
            If e.Column.FieldName = "ProjectDocument" Then
                aRTEdit = New RepositoryItemButtonRichTextEditExt
                If e.CellValue IsNot Nothing Then
                    aRTEdit.Text = e.CellValue.ToString
                End If
                aView.GridControl.RepositoryItems.Add(aRTEdit)
                e.RepositoryItem = aRTEdit
            End If
        End If

        Select Case aView.Tag.ToString
            Case "Activity"
                'in case of
            Case "ProjectResource"
                If e.Column.FieldName = "ProductId_FK" Then
                    aLov = e.RepositoryItem.Clone
                    aDt = aLov.DataSource

                    Dim result = (From oData In aDt.AsEnumerable()
                         Where oData.Field(Of Guid)("EmplID") = aRow("EmployeeId_FK")
                         Select oData Order By oData.Field(Of Date)("StartDate") Descending)

                    aLov.DataSource = result.CopyToDataTable
                    e.RepositoryItem = aLov
                End If
            Case Else
        End Select
    End Sub
    ''' <summary>
    ''' This event will fire when a ROW has been modifed and the user moved out from the row edited
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub RowNotify(sender As Object, e As RowObjectEventArgs)
        Dim aDataRow As DataRow = TryCast(e.Row, DataRowView).Row
        If aDataRow IsNot Nothing Then
            If aDataRow.IsColumnChanged("StatusID_FK") Then
                Me.aGridControl.SaveCommand()
                'MessageWindow.ShowMessage("Notify on Row:" + aDataRow.Table.TableName)
            End If
        End If
    End Sub

    'Pu utilisé....
    'Public Overridable Sub ShownDetailEditor(ByVal sender As Object, e As System.EventArgs)
    '    Dim columnView As ColumnView = TryCast(sender, ColumnView)

    '    If columnView IsNot Nothing Then
    '        Dim activeEditor As RichTextEdit = TryCast(columnView.ActiveEditor, RichTextEdit)
    '        Dim gpc As DevExpress.XtraRichEdit.Commands.ScrollToPageCommand
    '        If activeEditor IsNot Nothing Then
    '            Dim richEditControl As RichEditControl = CType(activeEditor.Controls(0), RichEditControl)
    '            ' TODO: Use any RichEditControl API
    '            richEditControl.ActiveViewType = RichEditViewType.PrintLayout
    '            richEditControl.ActiveView.ZoomFactor = 1.2
    '            'richEditControl.Document.Sections(0).Margins.Left = 50
    '            'richEditControl.Document.Sections(0).Margins.Top = 50
    '            richEditControl.Dock = DockStyle.Fill
    '            ''http://www.devexpress.com/Support/Center/p/Q256707.aspx
    '            gpc = New DevExpress.XtraRichEdit.Commands.ScrollToPageCommand(richEditControl) 'zero-based page index
    '            gpc.PageIndex = 0
    '            gpc.Execute()
    '        End If
    '    End If
    'End Sub

    Public Sub Init_GridActionOptionList(sender As Object, e As EventArgs)
        aGridControl.GridActionOptionList.Add(New ActionOption("Task", "ACT_00160", "Copy", AddressOf CopyActivity, True, True))

    End Sub


    'Public Sub Init_TreeActionOptionList(sender As Object, e As EventArgs)
    '    Me.TreeActionOptionList.Add(New ActionOption("Activity", "ACT_00066", "Last 90 Days", AddressOf MenuActivityLessThan90, True, True))
    '    Me.TreeActionOptionList.Add(New ActionOption("Activity", "ACT_00067", "Last 120 Days", AddressOf MenuActivityLessThan90, True, True))
    '    Me.TreeActionOptionList.Add(New ActionOption("Activity", "ACT_00068", "All", AddressOf MenuActivityLessThan90, True, True))
    '    Me.TreeActionOptionList.Add(New ActionOption("AddActivity", "ACT_00069", "New Activity", AddressOf MenuActivityLessThan90, True, True))
    'End Sub

    Public Sub MenuActivityLessThan90(ByVal sender As Object, ByVal e As EventArgs)
        MessageWindow.ShowMessage("InDev")
    End Sub

    Public Overridable Sub ProcessCurrentRecord(ByVal sender As Object, ByVal e As Views.Base.ValidateRowEventArgs, ByVal index As Integer)
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row

        Dim aEmplID As Guid
        Dim aPrdID As Guid
        Dim aEmpPrdDataRow As DataRow

        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "ProjectResource"
                    If Not IsDBNull(row("EmployeeId_FK")) AndAlso Not IsDBNull(row("ProductId_FK")) AndAlso IsDBNull(row("ProjectResourceRate")) Then
                        aEmplID = DirectCast(row("EmployeeId_FK"), Guid)
                        aPrdID = DirectCast(row("ProductId_FK"), Guid)
                        aEmpPrdDataRow = SystemBoxSDK.PL.TSMSDK.EmployeeProduct_PL.GetEmployeeProduct(aEmplID, aPrdID)
                        If aEmpPrdDataRow IsNot Nothing Then
                            row("ProjectResourceRate") = aEmpPrdDataRow("BillingRate")
                            'Default Value
                            If IsDBNull(row("DiscountedRate")) Then
                                row("DiscountedRate") = 0
                            End If
                        End If
                    End If
                Case Else
            End Select
        End If
    End Sub

    Public Overridable Sub ValidateRow(ByVal sender As Object, ByVal e As ValidateRowEventArgs)
        Dim row As DataRow = DirectCast(e.Row, DataRowView).Row
        If row IsNot Nothing Then
            Select Case row.Table.TableName
                Case "Activity"
                    Using plAct As SystemBoxSDK.PL.CRMSDK.Project_PL = New SystemBoxSDK.PL.CRMSDK.Project_PL()
                        plAct.ValidateRow(row)
                    End Using
                Case Else
            End Select
        End If
    End Sub
    ''' <summary>
    ''' Be able to print the Activity sheet according to the right GCCT sequence Documenttemplate report...
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SUB is not completed.... it's a HACK0 </remarks>
    Public Overridable Sub MenuPrintActivity(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        Dim aTreelistnode As TreeListNode = CType(CType(sender, DXMenuItem).Tag, TreeListNode)
        Dim activityID As Guid = CType(aTreelistnode.GetValue(KeyFieldName), Guid)

        Dim queryName As String = "ActivitySheet"
        Dim reportName As String = "ADSE"

        Using mquery As MetaQuery = MetaQuery.GetMetaQueryByName(queryName)
            If mquery IsNot Nothing Then
                Dim report As DevExpress.XtraReports.UI.XtraReport = SCIEF.ActiveFramework.CommonExt.RptNotification.GetReportFromDB(reportName)
                If report IsNot Nothing Then

                    'mquery.SQLWhere = mquery.SQLWhere + String.Format("GUIDFIELD = '{0}'", imExplorer.NodeID)  'avant mon passage. clieng 2013-110-11.
                    mquery.SQLWhere = mquery.SQLWhere + String.Format(mquery.GUIDField + " = '{0}'", activityID.ToString) 'imExplorer.NodeID

                    mquery.FillResultQuery()
                    report.DataSource = mquery.ResultQuery
                    report.CreateDocument()

                    Dim aFrm As Form = New ReportPrintForm() With {.Text = ".................", .Report = report}
                    aFrm.Show()
                Else
                    Console.WriteLine(String.Format("{0}, Not Found in Report Definition. Notification abord", reportName))
                End If
            Else
                Console.WriteLine(String.Format("{0}, Not Found in MetaCommand. Notification abord", queryName))
            End If
        End Using

        Me.Cursor = Cursors.Default
    End Sub


    ''' <summary>
    ''' CopyActivity
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>This SystemBOX function permit to copy an Activity to a new COPY of this Activity called COPY_ ...</remarks>
    Public Shared Sub CopyActivity(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim newName As String = String.Format("Copy of {0}", aDataRow("ProjectName"))

        Try
            Project_PL.CopyActivity(CType(aDataRow("ProjectId"), Guid), newName)
            MessageWindow.ShowMessage(String.Format("Copy of '{0}' To '{1}' Done", aDataRow("ProjectName"), newName), MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub

End Class

